﻿using Dapper;
using ProTrack.Dto;
using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IRepository.WareHouse;
using ProTrack.IServices.Stock;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.Stock
{
    public class OrderProductReceiveServices: IOrderProductReceiveServices
    {
        public OrderProductReceiveServices(IBaseRepository db, IOrderProductReceiveRepository or)
        {

            Or = or;
            Db = db;
        }

        public IOrderProductReceiveRepository Or { get; }
        public IBaseRepository Db { get; }

        //新增
        public async Task<bool> AddAsync(OrderProductReceive model, List<OrderProductReceiveItem> list)
        {
            var result = await Or.AddOrderProductReceive(model, list);
            return result;
        }
        //批量删除
        public async Task<bool> Delete(int[] Ids)
        {

            var result = await Or.Delete(Ids);
            return result;
        }
        //删除
        public async Task<bool> Delete(int Id)
        {
            var result = await Or.Delete(Id);
            return result;
        }
        //查询所有数据
        public async Task<IEnumerable<OrderProductReceive>> GetAllAsync()
        {
            string sql = "select * from OrderProductReceive";
            var list = await Db.QueryAllAsync<OrderProductReceive>(sql);
            return list;
        }
        //分页获取
        public async Task<IEnumerable<OrderProductReceive>> GetAsync(int PageIndex, int PageSize, string Number, string WarehouseId, string Status)
        {
            string sql = "select * from OrderProductReceive where 1=1 ";

            if (!string.IsNullOrEmpty(Number))
            {
                sql += " and Number like @Number ";
            }

            if (!string.IsNullOrEmpty(WarehouseId))
            {
                sql += " and WarehouseId like @WarehouseId ";
            }

            if (!string.IsNullOrEmpty(Status) && !Status.Equals("全部"))
            {
                sql += " and Status = @Status ";
            }



            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only ";

            var list = await Db.QueryAsync<OrderProductReceive>(sql, new { PageIndex, PageSize, Number = $"%{Number}%", WarehouseId = $"%{WarehouseId}%", Status = $"{Status}" });
            return list;
        }
        //获取总条数
        public async Task<int> GetCountAsync(string Number, string WarehouseId, string Status)
        {
            string sql = "select count(*) from OrderProductReceive where 1=1 ";
            if (!string.IsNullOrEmpty(Number))
            {
                sql += " and Number like @Number ";
            }

            if (!string.IsNullOrEmpty(WarehouseId))
            {
                sql += " and WarehouseId like @WarehouseId ";
            }

            if (!string.IsNullOrEmpty(Status))
            {
                sql += " and Status like @Status ";
            }



            var count = await Db.GetRecoreCountAsync(sql, new { Number = $"%{Number}%", WarehouseId = $"%{WarehouseId}%", Status = $"%{Status}%" });
            return count;
        }

        public async Task<IEnumerable<OrderProductReceiveItemDto>> GetOrderProductPickItemById(string OrderProductPickId, string WareHouseId)
        {
            var list = await Db.QueryAsync<OrderProductReceiveItemDto>(@"select ord.*,it.Id as ItemId,it.ItemCode,it.ItemName,o.WarehouseId,
                    it.Model,it.Spec,it.Kind,it.UnitOfMeasure,(isk.StockCount-isk.SalesCount) as CurrCount
                    from OrderProductPickItem ord
                    inner join OrderProductPick o on o.Id=ord.OrderProductPickId
                    inner join Item it on it.Id = ord.ItemId
                    inner join ItemStock isk on isk.ItemId = it.Id and isk.WareHouseId = @WareHouseId
                    where ord.OrderProductPickId =@OrderProductPickId", new { OrderProductPickId, WareHouseId });
            return list;
        }

        //获取产品信息
        public async Task<IEnumerable<OrderProductReceiveItemDto>> GetOrderProductReceiveItemById(string OrderProductReceiveId,string WareHouseId)
        {
            var list = await Db.QueryAsync<OrderProductReceiveItemDto>(@"select ord.*,it.Id as ItemId,it.ItemCode,it.ItemName,o.WarehouseId,
                    it.Model,it.Spec,it.Kind,it.UnitOfMeasure,(isk.StockCount-isk.SalesCount) as CurrCount
                    from OrderProductReceiveItem ord
                    inner join OrderProductReceive o on o.Id=ord.OrderProductReceiveId
                    inner join Item it on it.Id = ord.ItemId
                    inner join ItemStock isk on isk.ItemId = it.Id and isk.WareHouseId = @WareHouseId
                    where ord.OrderProductReceiveId =@OrderProductReceiveId", new { OrderProductReceiveId, WareHouseId });
            return list;
        }

        //修改
        public async Task<bool> UpdateAsync(OrderProductReceive model, List<OrderProductReceiveItem> list)
        {
            var result = await Or.UpdateOrderProductReceive(model, list);
            return result;
        }

        //修改单据状态
        public async Task<bool> UpdateStatus(long Id, int Status)
        {
            string sql = $"update OrderProductReceive set Status=@Status where Id=@Id";
            var result = await Db.UpdateAsync(sql, new { Id, Status });
            return result;
        }
    }
}
